Skip to content

PSBT fixes for 1.6.1 - #558

Closed
kkdao wants to merge 3 commits into
ElementsProject:post_1_6_0_updatesfrom
kkdao:fix/psbt-loose-parsing
Closed

kkdao wants to merge 3 commits into
ElementsProject:post_1_6_0_updatesfrom
kkdao:fix/psbt-loose-parsing

Conversation

@kkdao

@kkdao kkdao commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Three small PSBT fixes for 1.6.1, targeting post_1_6_0_updates as asked in
#556. Each is its own commit with a test that fails without the fix.

psbt: Refuse v2 global tx under loose parsing

wally_psbt_from_bytes() with WALLY_PSBT_PARSE_FLAG_LOOSE accepted a v2 PSBT
carrying PSBT_GLOBAL_UNSIGNED_TX when the tx key came before the version key.
The input and output arrays allocated for the tx were then overwritten by the
v2 path and leaked. BIP-370
says "PSBT_GLOBAL_UNSIGNED_TX must be excluded in PSBTv2", psbt_is_valid()
already rejects such a PSBT so it could not be serialized, and the reverse key
order was already refused.
The parser now refuses both orders.

Test: test_v2_global_tx_loose parses the version alone (accepted) and the tx
before and after the version (both refused).

psbt: Refuse upgrading a v0 PSBT with no tx

wally_psbt_set_version(psbt, 0, 2) on a v0 PSBT with no global tx read
psbt->tx without checking it and crashed. This is worse than I described in
#556: it does not need loose parsing. A v0 PSBT with no tx is valid and
wally_psbt_init_alloc(0, ...) returns one, so init then set_version crashes.
The upgrade now returns WALLY_EINVAL, the same way psbt_build_tx() and the
other v0 paths treat a missing tx.

Test: test_set_version_without_tx covers both the wally_psbt_init_alloc()
PSBT and the loosely parsed psbt\xff\x00, and checks that a no-op
set_version to 0 still succeeds and the version is unchanged after the refusal.

psbt: Copy the PSET genesis blockhash

wally_psbt_clone_alloc() and wally_psbt_combine() copied every global PSET
field except the genesis blockhash. A cloned PSET serialized without it, and
its taproot inputs could not be signed since the Elements taproot signature
hash requires it. It is now copied when the destination has none.

Test: test_pset_genesis_blockhash checks the clone serializes identically to
the source, that combine fills in a missing hash, and that combine keeps an
existing one.

Not included

Testing

src/test/test_psbt.py and src/ctest/test_psbt pass on each commit. On the
tip I ran, in Debian trixie on arm64: the --enable-elements=no -Werror
build, clang scan-build, ASan/UBSan make check with and without Elements, and
valgrind on the C tests plus test_psbt.py. arm64 gcc needs
-flax-vector-conversions -fsigned-char to build master at all (scrypt NEON
and the ccan base64 table), so those two flags were added to match x86 CI.

wally_psbt_from_bytes() with WALLY_PSBT_PARSE_FLAG_LOOSE accepted a v2
PSBT carrying PSBT_GLOBAL_UNSIGNED_TX if the tx key came before the
version key. psbt_set_global_tx() allocated the input and output arrays
for the tx, then the v2 path overwrote those pointers with new arrays,
leaking the old ones.

Refuse such a PSBT instead of freeing the arrays. A v2 PSBT with a tx
fails psbt_is_valid(), so it could not even be serialized, and the
reverse key order is already refused by psbt_set_global_tx(). The
signature-only PSBTs that loose parsing exists for never have a tx in
v2.
wally_psbt_set_version() upgrading a v0 PSBT to v2 read the version,
locktime, inputs and outputs from psbt->tx without checking it exists.
A v0 PSBT with no global tx is valid: wally_psbt_init_alloc() makes
one, and loose parsing accepts one, so set_version() on either
dereferenced NULL.

Refuse the upgrade with WALLY_EINVAL, as psbt_build_tx() and the other
v0 paths already do when there is no tx.
wally_psbt_clone_alloc() and wally_psbt_combine() copied every global
PSET field except the genesis blockhash, so a cloned PSET serialized
without it and its taproot inputs could no longer be signed, as the
Elements taproot signature hash requires the genesis blockhash.

Copy it when the destination has none. A clone always takes it from
the source, and a combine keeps an existing value.
@jgriffiths

Copy link
Copy Markdown
Contributor

Merged with minor changes, many thanks!

@jgriffiths jgriffiths closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants